﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Pathfinding;
using UnityEngine;

namespace Gp.Scripts.Core
{
    public class CellGridPath
    {
        private readonly ABPath _path;
        public CellGridPath(ABPath path)
        {
            _path = path;
        }
        
        public List<Vector3> VectorPath;
        public List<VBattleCell> VCells;
        public bool error => _path.error;

        public IEnumerator WaitForPath()
        {
            AstarPath.StartPath(_path);
            yield return _path.WaitForPath();
            VCells     = _path.path.Select(x => new VBattleCell(x)).ToList();
            VectorPath = VCells.Select(x => x.Position).ToList();
        }

        public void BlockUntilCalculated()
        {
            AstarPath.StartPath(_path);
            _path.BlockUntilCalculated();
            VCells     = _path.path.Select(x => new VBattleCell(x)).ToList();
            VectorPath = VCells.Select(x => x.Position).ToList();
        }
    }
}